import logging

import requests

from api.p2p_approve_new import ApproveTrust
from api.p2p_reg_login import RegLogin
from common import parser
from common.parser import parser_html


class TestApproveTrust(object):
    # 前置方法
    def setup(self):
        session = requests.Session()
        self.app_trust = ApproveTrust(session)
        self.reg_login = RegLogin(session)

        # 实名认证成功脚本

    def test01_approve_success(self):
        # 准备测试数据
        data1 = {"realname": "瑞文", "card_id": "150422199003077251"}
        data = {"keywords": "13900005001",
                "password": "Aa123456"}
        self.reg_login.login(data)
        # 调用接口方法
        resp = self.app_trust.approve(data1)
        # 打印结果
        logging.info(f"实名认证结果:{resp.json()}")
        # 断言结果
        assert 200 == resp.status_code
        assert 200 == resp.json().get("status")
        assert "提交成功" in resp.json().get("description")

    # 认证失败
    def test02_approve_no_name(self):
        # 准备测试数据
        data1 = {"realname": "", "card_id": "150422199003077251"}
        data = {"keywords": "13900005005",
                "password": "Aa123456"}
        self.reg_login.login(data)
        # 调用接口方法
        resp = self.app_trust.approve(data1)
        # 打印结果
        logging.info(f"实名认证结果:{resp.json()}")
        # 断言结果
        assert 200 == resp.status_code
        assert 100 == resp.json().get("status")
        assert "姓名不能为空" in resp.json().get("description")

    # def test03_approve_no_card(self):
    #     # 准备测试数据
    #     data = {"keywords": "13900005005",
    #                 "password": "Aa123456"}
    #     self.reg_login.login(data)
    #     # 调用接口方法
    #     resp = self.app_trust.trust()
    #     result = parser_html(resphone = resp)
    #     r = requests.post(url = result[0],data = result[1])
    #
    #     # 打印结果
    #     logging.info(f"第三方开户结果:{r.json()}")
    #     # 断言结果
    #     assert "OK" in r.test
    def test04_get_approve_ok(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}
        self.reg_login.login(data)
        resp = self.app_trust.get_approve()
        logging.info(f"获取认证状态结果：{resp.json()}")
        assert 200 == resp.status_code

    def test05_get_approve_faile(self):
        data = {"keywords": "13900005005",
                "password": "Aa123456"}
        self.reg_login.login(data)
        resp = self.app_trust.get_approve()
        logging.info(f"获取认证状态结果：{resp.json()}")
        assert 200 == resp.status_code

    def test06_trust_success(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}
        self.reg_login.login(data)
        resp = self.app_trust.trust()
        logging.info(f"后台开户结果：{resp.json()}")
        assert 200 == resp.status_code
        assert 200 == resp.json().get("status")
        assert "form" in resp.json().get("description")

    def test07_trust_success(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}
        self.reg_login.login(data)
        resp = self.app_trust.trust()
        result = parser_html(resp)
        r = requests.post(url=result[0], data=result[1])
        logging.info(f"后台开户结果是:{r.text}")
        # 断言结果
        assert "OK" in r.text

    def test08_recharge_code_success(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}

        self.reg_login.login(data)
        num = 123
        resp = self.app_trust.get_recharge_img(num)
        logging.info(f"获取充值结果：{resp}")
        assert 200 == resp.status_code

    def test09_recharge_code_float(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}

        self.reg_login.login(data)
        num = 0.123
        resp = self.app_trust.get_recharge_img(num)
        logging.info(f"获取充值结果：{resp}")
        assert 200 == resp.status_code

    def test10_recharge_code_no_number(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}

        self.reg_login.login(data)
        num = "aa"
        resp = self.app_trust.get_recharge_img(num)
        logging.info(f"获取充值结果：{resp}")
        assert 400 == resp.status_code

    def test11_recharge_code_no_number(self):
        data = {"keywords": "13900005001",
                "password": "Aa123456"}

        self.reg_login.login(data)
        num = "aa"
        resp = self.app_trust.get_recharge_img(num)
        logging.info(f"获取充值结果：{resp}")
        assert 400 == resp.status_code

    # def test12_recharge_success(self):
    #     data = {"keywords": "13900005001",
    #             "password": "Aa123456"}
    #
    #     self.reg_login.login(data)
    #     reqdata ={"paymentType": "chinapnrTrust",
    #                 "amount": 1000,
    #                 "formStr": "reForm",
    #                 "valicode": 8888}
    #
    #
    #     resp = self.app_trust.recharge(reqdata)
    #     logging.info(f"后台充值结果：{resp}")
    #     assert 200 == resp.status_code
    #     assert 200 == resp.json().get("status")
    #     assert "form" in resp.json().get("description")
    # def test13_recharge_faile(self):
    #     data = {"keywords": "13900005001",
    #             "password": "Aa123456"}
    #
    #     self.reg_login.login(data)
    #
    #     reqdata ={"paymentType": "chinapnrTrust",
    #                 "amount": 1000,
    #                 "formStr": "reForm",
    #                 "valicode": 6688}
    #
    #
    #     resp = self.app_trust.recharge(reqdata)
    #     logging.info(f"充值结果：{resp.json()}")
    #     assert 200 == resp.status_code
    #     assert 100 == resp.json().get("status")
    #     assert "验证码错误" in resp.json().get("description")
    # def test14_trust_success(self):
    #     data = {"keywords": "13900005001",
    #             "password": "Aa123456"}
    #     self.reg_login.login(data)
    #     reqdata = {"paymentType": "chinapnrTrust",
    #                "amount": 1000,
    #                "formStr": "reForm",
    #                "valicode": 8888}
    #     resp = self.app_trust.recharge(reqdata)
    #     resp = self.app_trust.trust()
    #     result = parser_html(response=resp)
    #     logging.info(f"第三方开户结果：{r.text}")
    #     assert "OK" in r.text
